package com.ganyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ganyu.domain.ResponseResult;
import com.ganyu.domain.entity.Comment;
import com.ganyu.domain.vo.CommentListVo;
import com.ganyu.domain.vo.PageVo;
import com.ganyu.exception.SystemException;
import com.ganyu.mapper.CommentMapper;
import com.ganyu.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ganyu.service.CommentService;
import org.springframework.util.StringUtils;

import java.util.List;

import static com.ganyu.constant.SystemConstant.ARTICLE_COMMENT;
import static com.ganyu.constant.SystemConstant.TO_COMMENT_ID;
import static com.ganyu.enums.AppHttpCodeEnum.CONTENT_ISNULL;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author chenxi
 * @since 2022-09-16 11:20:27
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserServiceImpl userServiceImpl;

    //显示评论
    @Override
    public ResponseResult<CommentListVo> getCommentList(String commentType,Long articleId, Integer pageNum, Integer pageSize) {
        //1.根据articleId获取评论，评论类型是友链的不用获取文章Id
        LambdaQueryWrapper<Comment> query = new LambdaQueryWrapper<>();
        query.eq(ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId);
        //2. 查询根评论，根评论的toCommentId为-1
        query.eq(Comment::getToCommentId,TO_COMMENT_ID);
        //3.查询评论类型
        query.eq(Comment::getType,commentType);
        //4. 最新评论显示在最上面
        query.orderByDesc(Comment::getCreateTime);
        //5. 进行分页操作
        Page<Comment> page = new Page<>(pageNum,pageSize);
        page(page, query);
        List<Comment> commentList = page.getRecords();
        //6.进行Bean拷贝
        List<CommentListVo> commentListVos = getCommentListVo(commentList);
        //7.获取子评论
        for(CommentListVo commentListVo : commentListVos){
            List<CommentListVo> children = getChildren(commentListVo.getId());
            commentListVo.setChildren(children);
        }
        PageVo<CommentListVo> pageVo = new PageVo<>();
        pageVo.setRows(commentListVos);
        pageVo.setTotal((int)page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    //添加评论
    @Override
    public ResponseResult<CommentListVo> addComment(Comment comment) {
        //1.评论不能为空
        if(!StringUtils.hasText(comment.getContent())){
            throw new SystemException(CONTENT_ISNULL);
        }
        // TODO 屏蔽敏感词

        save(comment);
        return ResponseResult.okResult();
    }

    //RootId相同的评论是同一个根评论下的子评论
    private List<CommentListVo> getChildren(Long id){
        LambdaQueryWrapper<Comment> query = new LambdaQueryWrapper<>();
        query.eq(Comment::getRootId,id);
        //对这些评论根据时间进行排序
        query.orderByDesc(Comment::getCreateTime);
        //根据id将子评论封装到一个list中
        List<Comment> list = list(query);
        //将list对象放进CommentListVo中
        List<CommentListVo> commentListVoList = getCommentListVo(list);
        return commentListVoList;

    }
    private <E> List<CommentListVo> getCommentListVo(List<E> list) {
        List<CommentListVo> commentListVos = BeanCopyUtils.copyBeanList(list,CommentListVo.class);
        //遍历commentListVos，根据createBy查找nickName，toCommentUserId为-1则不需要显示toCommentUserName
        for (CommentListVo commentListVo : commentListVos) {
            //找到username的id
            Long createBy = commentListVo.getCreateBy();
            //获取昵称
            String nickName = userServiceImpl.getById(createBy).getNickName();
            //获取头像
            String avatar = userServiceImpl.getById(createBy).getAvatar();
            commentListVo.setUsername(nickName);
            commentListVo.setAvatar(avatar);
            //toCommentUserId为-1则不需要显示toCommentUserName
            if(commentListVo.getToCommentId() != TO_COMMENT_ID){
                String toCommentUserName = userServiceImpl.getById(commentListVo.getToCommentUserId()).getNickName();
                commentListVo.setToCommentUserName(toCommentUserName);
            }
        }
        return commentListVos;
    }
}

